<template>
  <div class="table-box">
    <!-- 搜索表单 -->
    <el-form :model="searchForm" ref="searchFormRef" :inline="true" class="search-form">
      <el-form-item label="标准编码" prop="standardCode">
        <el-input v-model="searchForm.standardCode" placeholder="请输入标准编码" clearable />
      </el-form-item>
      <el-form-item label="标准名称" prop="standardName">
        <el-input v-model="searchForm.standardName" placeholder="请输入标准名称" clearable />
      </el-form-item>
      <el-form-item label="产品类别" prop="productCategory">
        <el-select v-model="searchForm.productCategory" placeholder="请选择产品类别" clearable style="width: 180px">
          <el-option label="电子产品" value="ELECTRONICS" />
          <el-option label="机械产品" value="MACHINERY" />
          <el-option label="化工产品" value="CHEMICAL" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="searchForm.status" placeholder="请选择状态" clearable style="width: 180px">
          <el-option label="启用" value="ACTIVE" />
          <el-option label="禁用" value="INACTIVE" />
          <el-option label="草稿" value="DRAFT" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="table-header">
      <el-button type="primary" :icon="CirclePlus" @click="handleAdd">新增标准</el-button>
      <el-button type="success" :icon="Download" @click="handleExport">导出</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%" v-loading="loading" border>
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="standardCode" label="标准编码" />
      <el-table-column prop="standardName" label="标准名称" />
      <el-table-column prop="productCategory" label="产品类别">
        <template #default="scope">
          <el-tag>{{ getCategoryText(scope.row.productCategory) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="version" label="版本号" />
      <el-table-column prop="checkItems" label="检测项目数" align="center">
        <template #default="scope">
          <el-badge :value="scope.row.checkItems" class="item-badge" />
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ getStatusText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="creator" label="创建人" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column prop="lastUpdateTime" label="最后更新时间" />
      <el-table-column label="操作" width="250" fixed="right">
        <template #default="scope">
          <el-button type="primary" link @click="handleView(scope.row)">查看</el-button>
          <el-button type="primary" link @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="success" link @click="handleCopy(scope.row)">复制</el-button>
          <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
      />
    </div>
  </div>
</template>

<script setup lang="ts" name="qualityStandard">
import { ref, reactive, onMounted } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage, ElMessageBox } from "element-plus";
import { CirclePlus, Download } from "@element-plus/icons-vue";

const loading = ref(false);
const tableData = ref([]);
const searchFormRef = ref<FormInstance>();

// 查询表单
const searchForm = reactive({
  standardCode: "",
  standardName: "",
  productCategory: "",
  status: ""
});

// 分页配置
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
});

// 获取类别文本
const getCategoryText = (category: string) => {
  const categoryMap: Record<string, string> = {
    ELECTRONICS: "电子产品",
    MACHINERY: "机械产品",
    CHEMICAL: "化工产品"
  };
  return categoryMap[category] || category;
};

// 获取状态类型
const getStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    ACTIVE: "success",
    INACTIVE: "warning",
    DRAFT: "info"
  };
  return statusMap[status] || "info";
};

// 获取状态文本
const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    ACTIVE: "启用",
    INACTIVE: "禁用",
    DRAFT: "草稿"
  };
  return statusMap[status] || status;
};

// 获取表格数据
const getTableData = async () => {
  loading.value = true;
  try {
    // 模拟数据
    const mockData = [
      {
        id: 1,
        standardCode: "QS001",
        standardName: "智能手机质检标准",
        productCategory: "ELECTRONICS",
        version: "v1.2",
        checkItems: 15,
        status: "ACTIVE",
        creator: "张三",
        createTime: "2024-01-10 09:00:00",
        lastUpdateTime: "2024-01-15 10:30:00"
      },
      {
        id: 2,
        standardCode: "QS002",
        standardName: "工业机器人质检标准",
        productCategory: "MACHINERY",
        version: "v2.0",
        checkItems: 25,
        status: "ACTIVE",
        creator: "李四",
        createTime: "2024-01-08 14:20:00",
        lastUpdateTime: "2024-01-12 16:45:00"
      },
      {
        id: 3,
        standardCode: "QS003",
        standardName: "化工原料质检标准",
        productCategory: "CHEMICAL",
        version: "v1.0",
        checkItems: 8,
        status: "DRAFT",
        creator: "王五",
        createTime: "2024-01-15 11:00:00",
        lastUpdateTime: "2024-01-15 11:00:00"
      }
    ];
    
    tableData.value = mockData;
    pagination.total = mockData.length;
  } catch (error) {
    console.error("获取质检标准失败:", error);
    ElMessage.error("获取质检标准失败");
  } finally {
    loading.value = false;
  }
};

// 查询
const handleSearch = () => {
  pagination.currentPage = 1;
  getTableData();
};

// 重置
const handleReset = () => {
  if (!searchFormRef.value) return;
  searchFormRef.value.resetFields();
  pagination.currentPage = 1;
  getTableData();
};

// 分页大小改变
const handleSizeChange = (val: number) => {
  pagination.pageSize = val;
  getTableData();
};

// 当前页改变
const handleCurrentChange = (val: number) => {
  pagination.currentPage = val;
  getTableData();
};

// 新增标准
const handleAdd = () => {
  ElMessage.info("新增质检标准功能开发中");
};

// 导出
const handleExport = () => {
  ElMessage.info("导出功能开发中");
};

// 查看详情
const handleView = (row: any) => {
  ElMessage.info(`查看标准 ${row.standardName} 详情`);
};

// 编辑标准
const handleEdit = (row: any) => {
  ElMessage.info(`编辑标准 ${row.standardName}`);
};

// 复制标准
const handleCopy = (row: any) => {
  ElMessage.info(`复制标准 ${row.standardName}`);
};

// 删除标准
const handleDelete = async (row: any) => {
  ElMessageBox.confirm(`确认删除质检标准 "${row.standardName}" 吗？`, "删除确认", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(async () => {
      try {
        ElMessage.success("质检标准删除成功");
        getTableData();
      } catch (error) {
        console.error("质检标准删除失败:", error);
        ElMessage.error("质检标准删除失败");
      }
    })
    .catch(() => {
      // 取消删除
    });
};

// 页面加载时获取数据
onMounted(() => {
  getTableData();
});
</script>

<style scoped>
.search-form {
  background-color: #fff;
  padding: 10px;
  border-radius: 4px;
  margin-bottom: 16px;
}

.table-header {
  margin-bottom: 16px;
  display: flex;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  flex-shrink: 0;
}

.item-badge {
  margin-right: 10px;
}
</style>